/* ----------------------------------
			   IMPORT
---------------------------------- */ 
; always remember to import Java classes that are being used in this Jess file
(import dk.dtu.is.model.iec61850.ln.Zcev)
(import dk.dtu.is.utility.jess.StartIec61850ServerPolling)

/* ----------------------------------
			   GLOBAL
---------------------------------- */ 
; here you can add where you have located your files or other utilities
; remember to use the double backslash as folder separator!
;(defglobal ?*MYFILESPATH* = "C:\\Users\\UncleTom\\workspaces\\myworkspaces\\myproject\\bin")

/* ----------------------------------
			   MODULES
---------------------------------- */ 
; you could use of using this module for your own functions or rules related to the Zcev object or the IEC 61850 server connection
;(defmodule Iec61850)

/* ----------------------------------
			   TEMPLATE
---------------------------------- */ 
; map the bean Zcev into a class. 
; this creates a template in Jess of the correspondent Java Object
; see 6.5 of "Jess In Action"
(defclass Zcev Zcev)

; a general other fact for simulation and test purpose
(deftemplate VoltageStatus (slot theStatus))
(deftemplate BatteryCharging)
 
/* ----------------------------------
			  FUNCTION
---------------------------------- */

/* 
This is used to have a console after the program runs
*/
(deffunction command-loop ()
    "Explicitly start a command loop from within the system"
    ((new jess.Jesp ((engine) getInputRouter "t") (engine)) parse TRUE))

(deffunction FUNLIB::myprint (?message)
	(printout t ?message crlf)
)

/* ----------------------------------
				RULES
---------------------------------- */
(defrule detect-max-values
    "Test1: If the voltage is higher than 360, voltage is high"
    (Zcev (vol ?value))
    (test (> ?value 360))
	=>
       (printout t ?value " is High Value "  (assert (VoltageStatus (theStatus "high"))) crlf)
)

(defrule detect-battery-charging
    "if the current is greater than 0, then the battery is charging"
    (Zcev (cur ?value))
    (test (> ?value 0))
	=>
       (printout t "The battery in charging with "?value " Ampere"  (assert (BatteryCharging)) crlf)
)

(defrule detect-norm-values
    "Test2: If the value is lower than 300, than it is a low value"
    (Zcev (vol ?value))
    (test (< ?value 300))
	=>
       (printout t ?value "is Low Value "  (assert (VoltageStatus (theStatus "low"))) crlf)
)

; ToBeDone: add the rule for the Voltage normal value


/* ----------------------------------
				ACTIONS
---------------------------------- */
; Actions: this part of the program contains commands to be executed by the jess interpreter.

;(watch all)

; load the userFunction into Jess
(load-function StartIec61850ServerPolling)
; call the user function
; it starts the connection with the server and pulls data that are pushed into the Zcev object
(start-iec61850-serverpolling)
(watch facts)
(run-until-halt)

(command-loop)